package com.project.bookstore.admin.controller;

import com.project.bookstore.book.pojo.BookDetailVO;
import com.project.bookstore.book.pojo.BookMultiConditionVO;
import com.project.bookstore.book.pojo.BookNameByKeyWordVO;
import com.project.bookstore.book.service.BookService;
import com.project.bookstore.common.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags="管理员模块")
@RestController
@RequestMapping("/admin/book")
public class AdminBookController {
    @Autowired
    private BookService bookService;

    @ApiOperation("批量删除")
    @PostMapping("/multiDeleteByIds")
    public CommonResult<Void> multiDeleteByIds(@RequestBody Long[] ids){
        return bookService.multiDeleteByIds(ids);
    }

    @ApiOperation("后台管理多条件查询")
    @PostMapping("/multiCondition")
    public CommonResult<List<BookMultiConditionVO>> multiConditionQuery(@RequestBody BookMultiConditionVO multiConditionVO){

        return bookService.getByMultiCondition(multiConditionVO);
    }

    @ApiOperation("通过ID批量禁用")
    @PutMapping("/multiDisEnable")
    private CommonResult<Void> multiDisEnable(@RequestBody Long[] ids){
        return bookService.multiDisEnableByIds(ids);
    }

    @ApiOperation("通过ID批量启用")
    @PutMapping("/multiEnable")
    private CommonResult<Void> multiEnable(@RequestBody Long[] ids){
        return bookService.multiEnableByIds(ids);
    }

    @ApiOperation("通过ID批量禁止销售")
    @PutMapping("/multiDisMarketable")
    private CommonResult<Void> multiDisMarketable(@RequestBody Long[] ids){
        return bookService.multiDisMarketableByIds(ids);
    }

    @ApiOperation("通过ID批量开启销售")
    @PutMapping("/multiMarketable")
    private CommonResult<Void> multiMarketable(@RequestBody Long[] ids){
        return bookService.multiMarketableByIds(ids);
    }

    @ApiOperation("通过ID删除书籍")
    @DeleteMapping("/{id:[0-9]+}")
    public CommonResult<Void> deleteBookDetailById(@PathVariable Long id){
        bookService.deleteBookDetailById(id);
        return CommonResult.ok();
    }

    @ApiOperation("通过书名获取书籍详情")
    @GetMapping("/{bookName:[a-zA-Z]+}")
    public CommonResult<BookDetailVO> getByBookName(@PathVariable String bookName) {
        return CommonResult.ok(bookService.getByBookName(bookName));
    }

    @ApiOperation("根据关键字获取书名列表")
    @GetMapping("/bookName/{keyword}")
    public CommonResult<List<BookNameByKeyWordVO>> getBookNameByBookName(
            @PathVariable String keyword) {
        return CommonResult.ok(bookService.getBookNameByBookName(keyword));
    }

    @ApiOperation("插入新的书籍")
    @PostMapping("/")
    public CommonResult<Void> insertBook(@RequestBody BookDetailVO bookDetailVO) {
        bookService.insert(bookDetailVO);
        return CommonResult.ok();
    }

    @ApiOperation("修改书籍")
    @PutMapping("/{id}")
    public CommonResult<Void> updateBookDetail(@RequestBody BookDetailVO bookDetailVO) {
        return bookService.updateBookDetailById(bookDetailVO);
    }

    @ApiOperation("根据ID获取书籍详情")
    @GetMapping("/{id:[0-9]+}")
    public CommonResult<BookDetailVO> selectBookDetail(@PathVariable Long id) {
        return CommonResult.ok(bookService.getBookDetailById(id));
    }
}
